@echo off
rem Set UTF-8 encoding
chcp 65001 >nul

rem Get the directory where the script is located (i.e., the project root directory)
set "PROJECT_ROOT=%~dp0"
rem Remove the trailing backslash
set "PROJECT_ROOT=%PROJECT_ROOT:~0,-1%"

echo Project root directory: %PROJECT_ROOT%
echo.

rem Check if uv is installed
echo Checking uv installation...
uv --version >nul 2>&1
if %ERRORLEVEL% NEQ 0 (
    echo Error: uv is not installed or not in PATH!
    echo Please install uv first: https://docs.astral.sh/uv/getting-started/installation/
    pause
    exit /b 1
) else (
    echo uv is available
)

rem Create virtual environment with specified Python version (if not exists)
echo.
echo Checking/Creating virtual environment...
if not exist "%PROJECT_ROOT%\.venv" (
    echo Creating virtual environment with Python 3.11...
    uv venv --python 3.11
    if %ERRORLEVEL% NEQ 0 (
        echo Error: Failed to create virtual environment!
        pause
        exit /b 1
    ) else (
        echo Virtual environment created successfully
    )
) else (
    echo Virtual environment already exists
)

rem Add requirements.txt dependencies to pyproject.toml if both files exist
echo.
if exist "%PROJECT_ROOT%\pyproject.toml" if exist "%PROJECT_ROOT%\requirements.txt" (
    echo Adding requirements.txt dependencies to pyproject.toml...
    uv add --requirements requirements.txt
    if %ERRORLEVEL% NEQ 0 (
        echo Warning: Failed to add some dependencies from requirements.txt to pyproject.toml
        echo Continuing with existing configuration...
    ) else (
        echo Successfully added requirements.txt dependencies to pyproject.toml
    )
)

rem Sync dependencies from pyproject.toml or requirements.txt
echo.
echo Installing/Syncing dependencies...
if exist "%PROJECT_ROOT%\pyproject.toml" (
    echo Using pyproject.toml for dependency management...
    uv sync
    if %ERRORLEVEL% NEQ 0 (
        echo Error: Failed to sync dependencies from pyproject.toml!
        pause
        exit /b 1
    )
) else if exist "%PROJECT_ROOT%\requirements.txt" (
    echo Using requirements.txt for dependency management...
    uv pip install -r requirements.txt
    if %ERRORLEVEL% NEQ 0 (
        echo Error: Failed to install dependencies from requirements.txt!
        pause
        exit /b 1
    )
) else (
    echo Warning: No pyproject.toml or requirements.txt found!
    echo Continuing without installing dependencies...
)

echo Dependencies installed/synced successfully

rem Create the output directory if it doesn't exist
echo.
if not exist "%PROJECT_ROOT%\output" (
    mkdir "%PROJECT_ROOT%\output"
    echo Created output directory: %PROJECT_ROOT%\output
) else (
    echo Output directory already exists
)

rem Display the startup command
echo.
echo Startup command: uv run python .\src\run_server.py
echo Starting the server...
echo.

rem Execute the startup command using uv run
uv run python .\src\run_server.py

rem Check if the server started successfully
if %ERRORLEVEL% NEQ 0 (
    echo.
    echo Error: Failed to start the server!
    pause
    exit /b 1
)